package com.campus.mapper.Impl;

import com.campus.common.StringUtils;
import com.campus.domain.Dto.ActivityCreateDto;
import com.campus.domain.Dto.ActivitySelectDto;
import com.campus.domain.Dto.ActivityUpdateDto;
import com.campus.domain.Vo.ActivityPageVo;
import com.campus.mapper.ActivityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Repository
@Slf4j
public class ActivityMapperImpl implements ActivityMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer addActivity(ActivityCreateDto activityCreateDto) {
        String sql = "insert into t_manager_activity (name,category,title,creator,abstract,detailed_desc,status,created_time,archived_time,pic_urls,start_time,end_time) " +
                "values (?,?,?,?,?,?,?,?,?,?,?,?)";

        Object[] args = new Object[]{
                activityCreateDto.getName(),
                activityCreateDto.getCategory(),
                activityCreateDto.getTitle(),
                activityCreateDto.getCreator(),
                activityCreateDto.getAbstracts(),
                activityCreateDto.getDetailedDesc(),
                0,
                new Date(new java.util.Date().getTime()),
                activityCreateDto.getArchivedTime(),
                activityCreateDto.getPicUrls(),
                activityCreateDto.getStartTime(),
                activityCreateDto.getEndTime()
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer updateActivity(ActivityUpdateDto activityUpdateDto) {
        String sql = "update t_manager_activity set name = ?, category = ?, title = ?, abstract = ?, detailed_desc = ?, archived_time = ?, pic_urls = ?, start_time = ?,end_time = ? " +
                "where id = ?";
        Object[] args = new Object[]{
                activityUpdateDto.getName(),
                activityUpdateDto.getCategory(),
                activityUpdateDto.getTitle(),
                activityUpdateDto.getAbstracts(),
                activityUpdateDto.getDetailedDesc(),
                activityUpdateDto.getArchivedTime(),
                activityUpdateDto.getPicUrls(),
                activityUpdateDto.getStartTime(),
                activityUpdateDto.getEndTime(),
                activityUpdateDto.getId()
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer updateStatus(Integer status, Integer id) {
        String sql = "update t_manager_activity set status = ? where id = ?";
        Object[] args = new Object[]{
                status,
                id
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public List<ActivityPageVo> page(ActivitySelectDto activitySelectDto, Integer pageNum, Integer pageSize) {
        String sql = "select * from t_manager_activity where 1 = 1 ";

        String name = activitySelectDto.getName();
        Long status = activitySelectDto.getStatus();

        Object[] args = new Object[]{};
        if(name != null && !name.equals("")){
            sql = sql + "and name like ? ";
            args = StringUtils.add(args,"%"+name+"%");
        }
        if(status != null){
            sql = sql + "and status = ? ";
            args = StringUtils.add(args,status);
        }

        sql = sql + "ORDER BY created_time DESC";

        if(pageNum != null && pageSize != null){
            sql = sql + " limit ?,? ";
            args = StringUtils.add(args,(pageNum-1)*pageSize);
            args = StringUtils.add(args,pageSize);
        }



        return jdbcTemplate.query(sql,args,rs -> {
            List<ActivityPageVo> list = new ArrayList<>();
            while(rs.next()){
                ActivityPageVo activity = new ActivityPageVo();
                activity.setId(rs.getLong("id"));
                activity.setName(rs.getString("name"));
                activity.setCategory(rs.getString("category"));
                activity.setTitle(rs.getString("title"));
                activity.setCreator(rs.getString("creator"));
                activity.setAbstracts(rs.getString("abstract"));
                activity.setPicUrls(rs.getString("pic_urls"));
                activity.setDetailedDesc(rs.getString("detailed_desc"));
                activity.setStartTime(rs.getDate("start_time"));
                activity.setEndTime(rs.getDate("end_time"));
                list.add(activity);
            }
            return list;
        });
    }

    @Override
    public Integer pageCount(ActivitySelectDto activitySelectDto) {
        String sql = "select count(*) from t_manager_activity where 1 = 1 ";

        String name = activitySelectDto.getName();
        Long status = activitySelectDto.getStatus();

        Object[] args = new Object[]{};
        if(name != null && !name.equals("")){
            sql = sql + "and name like ?";
            args = StringUtils.add(args,"%"+name+"%");
        }
        if(status != null){
            sql = sql + "and status = ?";
            args = StringUtils.add(args,status);
        }

        return jdbcTemplate.query(sql,args,rs -> {

            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;

        });
    }
}
